using System;
using System.Collections;
using UnityEngine;

public class ChallengeFailDialog : MonoBehaviour
{
	public UIAnimationElement inAnimation;

	public UIAnimationElement outAnimation;

	public UIAnimationElement switchScreenAnimation;

	public UIAnimationElement successAnimation;

	public GameObject priceGameObject;

	public GameObject freeGameObject;

	public UIWidget buttonWidget;

	public UILabel priceLabel;

	public UISprite fillSprite;

	protected bool animationPlaying;

	protected float time;

	public float allowedDurationToPress = 1f;

	public int priceForBalls = 2;

	public CurrencyType currencyForBalls = CurrencyType.ggdollars;

	protected bool canUpdateTime;

	protected Action<bool> onDone;

	public GameObject okButton;

	public UILabel titleLabel;

	public void Show(string title, Action<bool> onDone)
	{
		UITools.SetActive(okButton, false);
		this.onDone = onDone;
		base.gameObject.SetActive(true);
		UITools.ChangeText(priceLabel, priceForBalls.ToString());
		bool flag = false;
		UITools.SetActive(priceGameObject, !flag);
		UITools.SetActive(freeGameObject, flag);
		UITools.ChangeText(titleLabel, title);
		ResetTime();
		ShowAnimation(inAnimation, delegate
		{
			canUpdateTime = true;
		});
	}

	private void ResetTime()
	{
		time = 0f;
		canUpdateTime = false;
		SetFillSprite(time);
	}

	private void SetFillSprite(float time)
	{
		fillSprite.fillAmount = Mathf.Lerp(1f, 0f, time / allowedDurationToPress);
	}

	private void ShowAnimation(UIAnimationElement animation, Action onComplete)
	{
		StopAllCoroutines();
		Debug.Log("SHOW ANIMATION " + animation.gameObject.name);
		StartCoroutine(DoShowAnimation(animation, onComplete));
	}

	private IEnumerator DoShowAnimation(UIAnimationElement animation, Action onComplete)
	{
		animation.Play();
		animationPlaying = true;
		while (animation.isPlaying)
		{
			yield return null;
		}
		animationPlaying = false;
		if (onComplete != null)
		{
			onComplete();
		}
	}

	public void OnPurchaseBallsClick()
	{
		if (!animationPlaying)
		{
			canUpdateTime = false;
			GGPlayerSettings instance = GGPlayerSettings.instance;
			WalletManager walletManager = instance.walletManager;
			if (walletManager.CanBuyItemWithPrice(priceForBalls, currencyForBalls))
			{
				PurchaseBalls();
				return;
			}
			UITools.SetActive(okButton, true);
			TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
			tableTennisMainNavigation.PushCurrencyStore(currencyForBalls, OnPurchaseComplete);
		}
	}

	private void OnPurchaseComplete(bool success)
	{
		GGPlayerSettings instance = GGPlayerSettings.instance;
		WalletManager walletManager = instance.walletManager;
		UITools.SetActive(okButton, false);
		if (walletManager.CanBuyItemWithPrice(priceForBalls, currencyForBalls))
		{
			PurchaseBalls();
		}
		else
		{
			canUpdateTime = true;
		}
	}

	private void PurchaseBalls()
	{
		GGPlayerSettings instance = GGPlayerSettings.instance;
		WalletManager walletManager = instance.walletManager;
		walletManager.BuyItem(priceForBalls, currencyForBalls);
		TableTennisMainNavigation instance2 = TableTennisMainNavigation.instance;
		instance2.SetHeader(TableTennisMainNavigation.HeaderType.CoinsCashEnergy);
		TableTennisCoinController coinControlForCurrency = instance2.GetCoinControlForCurrency(currencyForBalls);
		coinControlForCurrency.AnimateSpendMoney(buttonWidget, priceForBalls, delegate
		{
			ShowAnimation(successAnimation, delegate
			{
				base.gameObject.SetActive(false);
				CallOnDone(true);
			});
		});
	}

	private void OnTimerDone()
	{
		ShowAnimation(outAnimation, delegate
		{
			CallOnDone(false);
		});
	}

	public void ShowSwitchScreenAnimation()
	{
		ShowAnimation(switchScreenAnimation, delegate
		{
			base.gameObject.SetActive(false);
		});
	}

	private void CallOnDone(bool purchased)
	{
		TableTennisMainNavigation instance = TableTennisMainNavigation.instance;
		instance.SetHeader(TableTennisMainNavigation.HeaderType.None);
		if (onDone != null)
		{
			Action<bool> action = onDone;
			onDone = null;
			action(purchased);
		}
	}

	private void Update()
	{
		if (!animationPlaying && canUpdateTime)
		{
			time += RealTime.deltaTime;
			SetFillSprite(time);
			if (time > allowedDurationToPress)
			{
				canUpdateTime = false;
				OnTimerDone();
			}
		}
	}

	public void OnOk()
	{
		canUpdateTime = false;
		CallOnDone(false);
	}
}
